package com.treetao.union.mall.api.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.treetao.union.core.exception.DevMException;
import com.treetao.union.db.model.MallCart;
import com.treetao.union.db.model.vo.RestResponse;
import com.treetao.union.db.service.MallCartService;
import com.treetao.union.mall.api.base.BaseCURDController;
import com.treetao.union.mall.api.vo.req.ReqParam;

/**
 * 购物车
 * 
 * @author devm
 *
 */
@RestController
@RequestMapping("/mall/cart")
public class MallCartController extends BaseCURDController {

	@Autowired
	private MallCartService mallCartService;

	@Override
	protected IService<?> getCommonService() {
		return mallCartService;
	}

	//列表查询条件
	@Override
	protected void commonListWhere(QueryWrapper<?> q, ReqParam param) {
		q.eq("ref_user_id", getWxuidFromToken());
	}

	@GetMapping("/count")
	public RestResponse count() {
		QueryWrapper<MallCart> q = getQueryWrapperNoDel();
		q.eq("ref_user_id", getWxuidFromToken());
		return RestResponse.success(mallCartService.count(q));
	}

	/**
	 * 修改购物车数量
	 * 
	 * @param param
	 * @return
	 */
	@PostMapping("/changeNum")
	public RestResponse changeNum(@RequestBody ReqParam param) {
		Integer num = param.getInt("num", false);
		Long cartId = param.getLong("cartId", false);
		if (num <= 0) {
			throw new DevMException("数量必须大于1");
		}
		Long userId = getWxuidFromToken();
		MallCart cart = mallCartService.getById(cartId);
		if (cart != null && cart.getRefUserId().longValue() == userId.longValue()) {
			cart.setNum(num);
			mallCartService.updateById(cart);
			return RestResponse.success();
		}
		return RestResponse.failure("不存在记录");
	}

	/**
	 * 购物车删除商品
	 * 
	 * @param param
	 * @return
	 */
	@PostMapping("/remove")
	public RestResponse remove(@RequestBody ReqParam param) {
		Long cartId = param.getLong("cartId", false);
		Long userId = getWxuidFromToken();
		MallCart cart = mallCartService.getById(cartId);
		if (cart != null && cart.getRefUserId().longValue() == userId.longValue()) {
			mallCartService.removeById(cartId);
			return RestResponse.success();
		}
		return RestResponse.failure("不存在记录");
	}

	/**
	 * 添加到购物车
	 * 
	 * @return
	 */
	@PostMapping("/add")
	public RestResponse add(@RequestBody ReqParam param) {
		Long spuId = param.getLong("spuId", false);
		Long skuId = param.getLong("skuId", false);
		String skuName = param.getValue("skuName", false);
		String img = param.getValue("img", false);
		String attrName = param.getValue("attrName", false);
		int num = param.getInt("num", false);
		double price = param.getDouble("price", false);
		Long userId = getWxuidFromToken();

		// 如果已存在，则把数量加上去
		QueryWrapper<MallCart> q = getQueryWrapperNoDel();
		q.eq("ref_sku_id", skuId);
		q.eq("ref_user_id", userId);
		MallCart cart = mallCartService.getOne(q);
		if (cart != null) {
			cart.setNum(cart.getNum() + num);
		} else {
			cart = new MallCart();
			cart.setNum(num);
		}

		cart.setAttrName(attrName);
		cart.setName(skuName);
		cart.setPrice(price);
		cart.setRefSkuId(skuId);
		cart.setRefSpuId(spuId);
		cart.setRefUserId(userId);
		cart.setImg(img);
		boolean flag = mallCartService.saveOrUpdate(cart);
		return RestResponse.success().setDataOnlyResult(flag);
	}

}
